package com.hhsjdesign.dynamicui.reddot

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.widget.LinearLayout
import com.hhsjdesign.dynamicui.interfaces.IRedDot
import com.hhsjdesign.dynamicui.utils.RedDotUtil

/**
 * 可加载背景的布局
 */
class RedDotLinearLayout : LinearLayout, IRedDot {
    private val redDotController: RedDotController

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int) : super(
        context,
        attrs,
        0
    )

    init {
        setWillNotDraw(false)
        redDotController =
            RedDotController(30, 30, 5, 0xffff3333.toInt())
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        registerMsg()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        unRegisterMsg()
    }

    override fun onDrawForeground(canvas: Canvas?) {
        super.onDrawForeground(canvas)
        redDotController.onDrawForeground(canvas)
    }

    override fun removeRedDot() {
        redDotController.removeRedDor(this)
    }

    override fun showRedDot(msgTags: List<String>) {
        redDotController.showRedDor(msgTags, this)
    }

    override fun removeRedDot(msgTags: List<String>) {
        redDotController.removeRedDor(msgTags, this)
    }

    override var redDotTags: MutableList<String>
        get() = redDotController.tags!!
        set(value) {
            redDotController.tags = value
        }

    override fun isShowRedDot(): Boolean = redDotController.isShow()

    override fun redDotCircle(ox: Int, oY: Int) {
        redDotController.circle(ox, oY, this)
    }

    override fun registerMsg() {
        RedDotUtil.registerMsg(this);
    }

    override fun unRegisterMsg() {
        RedDotUtil.unRegisterMsg(this);
    }

}